package cn.com.git.example.common.base;

import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 基础分页查询
 * @author zhangzhenfu
 * @Description
 * @date 2020/4/3 15:16
 */
@Data
public abstract class BasePageQuery implements Serializable {

    private static final long serialVersionUID = 6186732444250870665L;

    /**
     * 是否排序,默认为false
     */
    protected Boolean needOrder = new Boolean(false);

    /**
     * 排序规则
     */
    protected List<SortParam> sortParams;

    public BasePageQuery() {
    }

    @Data
    public static class SortParam implements Serializable {
        /**
         * 排序字段
         */
        private String field;

        /**
         * 排序规则
         */
        private String sortType;

        public SortParam() {
        }

        public SortParam(String field, String sortType) {
            this.field = field;
            this.sortType = sortType;
        }
    }

    /**
     * 新增排序规则
     *
     * @param field    排序字段
     * @param sortType 排序类型
     */
    public void addSortParam(String field, String sortType) {
        if (StringUtils.isEmpty(field) || StringUtils.isEmpty(sortType)) {
            throw new NullPointerException();
        }
        this.needOrder = true;
        if (null == sortParams) {
            sortParams = new ArrayList<>();
        }
        SortParam sortParam = new SortParam(field, sortType);
        sortParams.add(sortParam);
    }

}
